package com.yoomiito.app.widget.dialog

import SddShareView
import SddShareView_2
import android.Manifest
import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import com.umeng.socialize.bean.SHARE_MEDIA
import com.yoomiito.app.R
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.model.bean.UmShareInfo
import com.yoomiito.app.model.sdd.ShareMsg
import com.yoomiito.app.utils.*
import com.yoomiito.app.utils.wx.UMengShareUtils

class ShareSendMoreAdDialog(mContext:Context,val goodsDetail: GoodsDetail?,val msg: ShareMsg,val type:Int) :BaseDialog(mContext, R.style.dialog_2){
    companion object{
        const val GoodsDetailType = 1
        const val GoodsListType = 2
    }
    lateinit var adView:View
    lateinit var bitmap:Bitmap
    override fun setView(): View {

        val view = LayoutInflater.from(mContext).inflate(R.layout.dialog_share_sdd_ad,null,false)
        view.findViewById<ImageView>(R.id.adPic).setImageBitmap(bitmap)
        view.findViewById<LinearLayout>(R.id.parentAd).setOnClickListener { dismiss() }
        view.findViewById<ImageView>(R.id.share_wx).setOnClickListener {
            //小程序的形式分享出去
            val info = UmShareInfo()
            if (type== GoodsDetailType){
                info.shareUrl = goodsDetail?.share_url
                info.title = goodsDetail?.title
                info.imgUrl = if(goodsDetail?.share_img?.isEmpty()!!)goodsDetail?.pict_url else goodsDetail?.share_img
                ///pages/sendmore/share?inviter=${minfo.invitation_code}&shareId=${shareId}
//                info.path = "/pages/index/mpcode?scene=${UserInfoConfig.getInviteCode()}_${goodsDetail?.num_iid}"
                info.path = "/pages/sendmore/share?inviter=${UserInfoConfig.getInviteCode()}&shareId=${msg.shareID}&id=${goodsDetail?.num_iid}"
            }else{
                info.shareUrl = "https://mimitao-cdn.aijiamai.com/assets/banbendic01b7baa68956.png"
                info.title = "优米·送多多平台补贴100万超值品牌商品包邮免费领取"
                info.imgUrl = "https://mimitao-cdn.aijiamai.com/static/share_img.png"
                info.path = "/pages/sendmore/share?inviter=${UserInfoConfig.getInviteCode()}&shareId=${msg.shareID}"
            }

            info.id = "gh_fdaef843ac93"
            UMengShareUtils.shareWXMiniProgram(mContext as Activity,info)
//            UMengShareUtils.shareWXPic(SHARE_MEDIA.WEIXIN,mContext as Activity,bitmap)
        }
        view.findViewById<ImageView>(R.id.share_wx_circle).setOnClickListener {
            UMengShareUtils.shareWXPic(SHARE_MEDIA.WEIXIN_CIRCLE,mContext as Activity,bitmap)
        }
        view.findViewById<ImageView>(R.id.download).setOnClickListener {
            if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
                AllUtils.requestPermission(mContext as AppCompatActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE).subscribe {
                    if(it){
                        ImageLoader.getInstance().downloadPic(mContext, bitmap)
                        YMToast.showCenterToast("保存成功")
                    }
                }
            }else{
                ImageLoader.getInstance().downloadPic(mContext, bitmap)
            }
        }
        setFullScreen(true)
        setCanceledOnTouchOutside(true)
        return view
    }

    override fun show() {
        if (type == GoodsDetailType){
            val view = SddShareView(mContext)
            view.setViewData(goodsDetail!!,msg)
            view.makeViewListener={view, bitmap ->
                adView = view
                this.bitmap = bitmap
                super.show()
            }
        }else{
            val view =  SddShareView_2(mContext)
            view.setViewData(msg)
            view.makeViewListener={view, bitmap ->
                adView = view
                this.bitmap = bitmap
                super.show()
            }
        }
    }
}